package spectra.ee.web.common.service;

import java.sql.SQLException;
import java.util.List;

import spectra.base.log.Logger;
import spectra.base.sql.ibatis.SqlMapperWeb;
import spectra.base.util.DateUtil;
import spectra.base.util.Util;
import spectra.ee.web.WebPublic;
import spectra.ee.web.common.model.Reply;

import com.ibatis.sqlmap.client.SqlMapClient;



/**
 * 댓글 관리 클래스.
 *
 * @author shson
 */
public class ReplyServiceBase
{

    /** Logger객체. */
    private static Logger logger = Logger.getLoggerInstance(ReplyServiceBase.class.getName());

    /** SqlMapClient 객체. */
    protected static SqlMapClient sqlMap = SqlMapperWeb.getSqlMapWebInstance();

    /**
    * 하나의 게시물(QnA 혹은 Faq)에 대한 댓글 목록을 가져오는 메소드.
    *
    * @param kbId 지식ID.
    * @param serviceType 서비스타입.
    *
    * @return CaseInsensitiveMap List.<br>
    * kb_id:지식ID.<br>
    * service_type:서비스타입.<br>
    * reply_seq:댓글시퀀스.<br>
    * reply:댓글.<br>
    * delete_flag:삭제플래그.<br>
    * created_by:등록자.<br>
    * created_name:등록자이름.<br>
    * created_type:등록타입.<br>
    * created_date:등록일자.<br>
    * updated_by:수정자.<br>
    * updated_name:수정자이름.<br>
    * updated_type:수정타입.<br>
    * updated_date:수정일자.<br>
    */
    public List selectReplyListById(String kbId, String serviceType)
    {
        List list = null;
        Reply obj = new Reply();
        obj.setKbId(kbId);
        obj.setServiceType(serviceType);
        try
        {
            list = (List) sqlMap.queryForList("Reply.selectReplyListById", obj);
        }
        catch(SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return list;
    }

    /**
    * reply_id의 replyseq 최대값을 가져오는 메소드.
    *
    * @param kbId 지식ID.
    * @param serviceType 서비스타입.
    *
    * @return MAX(댓글수).
    *
    * @throws Exception the exception
    */
    public int selectMaxReplySeq(String kbId, String serviceType) throws Exception
    {
        int iReplySerial = 0;
        Reply obj = new Reply();
        obj.setKbId(kbId);
        obj.setServiceType(serviceType);
        try
        {
            iReplySerial = Util.str2i(sqlMap.queryForObject("Reply.selectMaxReplySeq", obj) + "");
        }
        catch(SQLException e)
        {
            e.printStackTrace();
        }

        return iReplySerial;
    }

    /**
    * 댓글 입력하는 메소드.
    *
    * @param replyId 지식ID.
    * @param reply 댓글.
    * @param replySeq 댓글시퀀스
    * @param serviceType 서비스타입.
    * @param userId 사용자ID.
    * @param userName 사용자명.
    * @return 성공시 true, 실패시 false
    */
    public boolean insertReply(String replyId, String serviceType, String reply, int replySeq, String userId, String userName)
    {
        boolean bSuccess = false;
        Reply obj = new Reply();
        obj.setKbId(replyId);
        obj.setServiceType(serviceType);
        obj.setReplySeq(replySeq);
        obj.setReply(reply);
        obj.setDeleteFlag(WebPublic.FLAG_N);
        obj.setCreatedBy(userId);
        obj.setCreatedName(userName);
        obj.setCreatedType(WebPublic.TYPE_CUSTOMER);
        obj.setCreatedDate(DateUtil.getCurrDateTimeStamp());
        obj.setUpdatedBy(userId);
        obj.setUpdatedName(userName);
        obj.setUpdatedType(WebPublic.TYPE_CUSTOMER);
        obj.setUpdatedDate(DateUtil.getCurrDateTimeStamp());

        try
        {
            sqlMap.startTransaction();
            sqlMap.insert("Reply.insertReply", obj);
            sqlMap.commitTransaction();
        }
        catch(SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            try
            {
                sqlMap.endTransaction();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        bSuccess = true;
        return bSuccess;
    }

    /**
    * 댓글 수정하는 메소드.
    *
    * @param replyId 지식ID.
    * @param replySeq 댓글시퀀스
    * @param serviceType 서비스타입.
    * @param reply 댓글.
    * @param userId 사용자ID.
    * @param userName 사용자명.
    *
    * @return 적용 row 수.
    */
    public int updateReply(String replyId, String serviceType, String reply, int replySeq, String userId, String userName)
    {
        int iResult = -1;
        Reply obj = new Reply();
        obj.setKbId(replyId);
        obj.setServiceType(serviceType);
        obj.setReplySeq(replySeq);
        obj.setReply(reply);
        obj.setUpdatedBy(userId);
        obj.setUpdatedName(userName);
        obj.setUpdatedType(WebPublic.TYPE_CUSTOMER);
        obj.setUpdatedDate(DateUtil.getCurrDateTimeStamp());

        try
        {
            sqlMap.startTransaction();
            iResult = sqlMap.update("Reply.updateReply", obj);
            sqlMap.commitTransaction();
        }
        catch(SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            try
            {
                sqlMap.endTransaction();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }

        return iResult;
    }

    /**
    * 댓글 삭제하는 메소드.
    *
    * @param replyId 댓글ID.
    * @param serviceType 서비스타입.
    * @param replySeq 댓글시퀀스.
    *
    * @return 적용 row 수.
    */
    public int deleteReply(String replyId, String serviceType, int replySeq)
    {
        int iResult = -1;

        Reply obj = new Reply();
        obj.setKbId(replyId);
        obj.setServiceType(serviceType);
        obj.setReplySeq(replySeq);

        try
        {
            sqlMap.startTransaction();
            iResult = sqlMap.delete("Reply.deleteReply", obj);
            sqlMap.commitTransaction();
        }
        catch(SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        finally
        {
            try
            {
                sqlMap.endTransaction();
            }
            catch(SQLException e)
            {
                e.printStackTrace();
            }
        }
        return iResult;
    }

    /**
    * 댓글의 주인이 현재 로그인된 사용자인지를 체크하는 메소드.
    *
    * @param replyId 댓글ID.
    * @param serviceType 서비스타입.
    * @param replySeq 댓글시퀀스.
    * @param userId 사용자ID.
    * @return 로그인된 사용자이면 true, 아니면 false
    */
    public boolean selectReplyUserCheck(String replyId, String serviceType, int replySeq, String userId)
    {
        boolean bUserCheck = false;
        Reply obj = new Reply();
        obj.setKbId(replyId);
        obj.setServiceType(serviceType);
        obj.setCreatedType(WebPublic.TYPE_CUSTOMER);

        obj.setReplySeq(replySeq);
        obj.setCreatedBy(userId);
        try
        {
            Integer iReplyCount = (Integer) sqlMap.queryForObject("Reply.selectReplyUserCheck", obj);
            if (!new Integer(0).equals(iReplyCount))
            {
                bUserCheck = true;
            }
        }
        catch(SQLException e)
        {
            logger.error(e.getMessage());
            e.printStackTrace();
        }
        return bUserCheck;
    }

}
